﻿using System;

namespace Evolus.Mercury.Model
{
    /// <summary>
    /// Contains market information of a single symbol
    /// </summary>
    public class StockInfo
    {
        public int Id { get; set; }
        public Stock Stock { get; set; }
        public DateTime Date { get; set; }
        
        public decimal CeilingPrice { get; set; }
        public decimal ReferencePrice { get; set; }
        public decimal FloorPrice { get; set; }

        public decimal BuyPrice1 { get; set; }
        public int BuyVolume1 { get; set; }
        public decimal BuyPrice2 { get; set; }
        public int BuyVolume2 { get; set; }
        public decimal BuyPrice3 { get; set; }
        public int BuyVolume3 { get; set; }

        public decimal SellPrice1 { get; set; }
        public int SellVolume1 { get; set; }
        public decimal SellPrice2 { get; set; }
        public int SellVolume2 { get; set; }
        public decimal SellPrice3 { get; set; }
        public int SellVolume3 { get; set; }

        public decimal ClosingPrice { get; set; }

        public decimal HighPrice { get; set; }
        public decimal LowPrice { get; set; }

        public decimal LastTradedPrice { get; set; }
        public int LastTradedVolume { get; set; }

        public int TotalMatchedVolume { get; set; }

        public DateTime LastUpdatedTime { get; set; }

        public decimal Change
        {
            get { return LastTradedPrice - ReferencePrice; }
        }

        public ChangeType ChangeType
        {
            get { return GetChangeType(LastTradedPrice); }
        }

        public ChangeType GetChangeType(decimal price)
        {
            if (price == FloorPrice)
            {
                return ChangeType.Floor;
            }
            else if (price == CeilingPrice)
            {
                return ChangeType.Ceiling;
            }
            else if (price > ReferencePrice)
            {
                return ChangeType.Up;
            }
            else if (price < ReferencePrice)
            {
                return ChangeType.Down;
            }
            else
            {
                return ChangeType.Freeze;
            }
        }        

        internal bool UpdateWith(StockInfo that)
        {
            bool changed = false;
            if (this.CeilingPrice != that.CeilingPrice)
            {
                changed = true;
                this.CeilingPrice = that.CeilingPrice;
            }
            if (this.ReferencePrice != that.ReferencePrice)
            {
                changed = true;
                this.ReferencePrice = that.ReferencePrice;
            }
            if (this.FloorPrice != that.FloorPrice)
            {
                changed = true;
                this.FloorPrice = that.FloorPrice;
            }
            if (this.BuyPrice1 != that.BuyPrice1)
            {
                changed = true;
                this.BuyPrice1 = that.BuyPrice1;
            }
            if (this.BuyVolume1 != that.BuyVolume1)
            {
                changed = true;
                this.BuyVolume1 = that.BuyVolume1;
            }
            if (this.BuyPrice2 != that.BuyPrice2)
            {
                changed = true;
                this.BuyPrice2 = that.BuyPrice2;
            }
            if (this.BuyVolume2 != that.BuyVolume2)
            {
                changed = true;
                this.BuyVolume2 = that.BuyVolume2;
            }
            if (this.BuyPrice3 != that.BuyPrice3)
            {
                changed = true;
                this.BuyPrice3 = that.BuyPrice3;
            }
            if (this.BuyVolume3 != that.BuyVolume3)
            {
                changed = true;
                this.BuyVolume3 = that.BuyVolume3;
            }

            if (this.LastTradedPrice != that.LastTradedPrice)
            {
                changed = true;
                this.LastTradedPrice = that.LastTradedPrice;
            }
            if (this.LastTradedVolume != that.LastTradedVolume)
            {
                changed = true;
                this.LastTradedVolume = that.LastTradedVolume;
            }

            if (this.SellPrice1 != that.SellPrice1)
            {
                changed = true;
                this.SellPrice1 = that.SellPrice1;
            }
            if (this.SellVolume1 != that.SellVolume1)
            {
                changed = true;
                this.SellVolume1 = that.SellVolume1;
            }
            if (this.SellPrice2 != that.SellPrice2)
            {
                changed = true;
                this.SellPrice2 = that.SellPrice2;
            }
            if (this.SellVolume2 != that.SellVolume2)
            {
                changed = true;
                this.SellVolume2 = that.SellVolume2;
            }
            if (this.SellPrice3 != that.SellPrice3)
            {
                changed = true;
                this.SellPrice3 = that.SellPrice3;
            }
            if (this.SellVolume3 != that.SellVolume3)
            {
                changed = true;
                this.SellVolume3 = that.SellVolume3;
            }

            if (this.HighPrice != that.HighPrice)
            {
                changed = true;
                this.HighPrice = that.HighPrice;
            }
            if (this.LowPrice != that.LowPrice)
            {
                changed = true;
                this.LowPrice = that.LowPrice;
            }
            if (this.ClosingPrice != that.ClosingPrice)
            {
                changed = true;
                this.ClosingPrice = that.ClosingPrice;
            }

            if (this.TotalMatchedVolume != that.TotalMatchedVolume)
            {
                changed = true;
                this.TotalMatchedVolume = that.TotalMatchedVolume;
            }

            return changed;
        }
    }
}

